package com.ktwlsoft.archivemanage.service.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * DateUtils
 *
 * @author zhuhe on 2019/4/16 11:21
 * @version 1.0
 */
public class DateUtils {

    /** 日志对象 **/
    private static Logger logger = LoggerFactory.getLogger(DateUtils.class);

    /**年月日,时分秒*/
    public static final String ALLSF_PATTERN = "yyyy-MM-dd HH:mm:ss";
    /**年月日,时分*/
    public static final String ALLSF_NO_SECEND_PATTERN = "yyyy-MM-dd HH:mm";
    /**年月日*/
    public static final String SIMPLESF_PATTERN = "yyyy-MM-dd";

    /**
     * 获取转换器 on 2019/4/16 11:31
     *
     * @param pattern pattern
     * @return SimpleDateFormat
     */
    private static SimpleDateFormat getSdf(String pattern){
        return new SimpleDateFormat(pattern);
    }

    /**
     * string转date on 2019/4/16 11:30
     *
     * @param str str
     * @return Date
     */
    public static Date strToDate(String str){
        Date date = null;
        if(str == null || "".equals(str)){
            return null;
        }
        try {
            if(str.length() > 10){
                date = getSdf(ALLSF_PATTERN).parse(str);
            } else {
                date = getSdf(SIMPLESF_PATTERN).parse(str);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return date;
    }

    /**
     * date转string on 2019/4/16 11:30
     *
     * @param date date
     * @return String
     */
    public static String dateToStr(Date date,String dateType){
        String str = "";
        try {
            str = getSdf(dateType).format(date);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return str;
    }

    /**
     * date转string on 2019/4/16 11:30
     *
     * @param date date
     * @return String
     */
    public static String dateToStr(Date date){
        String str = "";
        try {
            str = getSdf(ALLSF_PATTERN).format(date);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return str;
    }

    /**
     * 两个时间相差的年数 on 2019/5/29 14:54
     *
     * @param startDate startDate
     * @param endDate endDate
     * @return int
     */
    public static int getTowDateYear(Date startDate, Date endDate){
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(getSdf(SIMPLESF_PATTERN).parse(getSdf(SIMPLESF_PATTERN).format(startDate)));
            c2.setTime(getSdf(SIMPLESF_PATTERN).parse(getSdf(SIMPLESF_PATTERN).format(endDate)));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        int result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
        return result == 0 ? 1 : Math.abs(result);
    }

    /**
     * 两个时间相差的月数 on 2019/5/29 14:54
     *
     * @param startDate startDate
     * @param endDate endDate
     * @return int
     */
    public static int getTowDateMonth(Date startDate, Date endDate){
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(getSdf(SIMPLESF_PATTERN).parse(getSdf(SIMPLESF_PATTERN).format(startDate)));
            c2.setTime(getSdf(SIMPLESF_PATTERN).parse(getSdf(SIMPLESF_PATTERN).format(endDate)));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        int result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        return result == 0 ? 1 : Math.abs(result);
    }

    /**
     * 获取两个时间相隔的天数 on 2019/5/29 14:54
     *
     * @param startDate startDate
     * @param endDate endDate
     * @return long
     */
    public static Integer getTowDateDay(Date startDate, Date endDate){
        Calendar beginCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        beginCalendar.setTime(startDate);
        endCalendar.setTime(endDate);
        long beginTime = beginCalendar.getTime().getTime();
        long endTime = endCalendar.getTime().getTime();
        return Integer.parseInt(String.valueOf((endTime - beginTime) / (1000 * 60 * 60 *24)));
    }

    /**
     * 得到几天前的时间 on 2019/5/29 14:54
     *
     * @param d d
     * @param day day
     * @return Date Date
     */
    public static Date getDateBefore(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
        return now.getTime();
    }

    /**
     * 得到几天后的时间 on 2019/5/29 14:54
     *
     * @param d d
     * @param day day
     * @return Date Date
     */
    public static Date getDateAfter(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
        return now.getTime();
    }

    /**
     * 获取当天的最小时间 on 2019/5/29 14:54
     *
     * @param time time
     * @return String
     */
    public static String dayMinTime(String time){
        if(time == null || "".equals(time.trim())){
            return time;
        }
        if(time.length() <= 10){
            time = time + " " + "00:00:00";
        }
        return time;
    }

    /**
     * 获取当天的最大时间 on 2019/5/29 14:54
     *
     * @param time time
     * @return String
     */
    public static String dayMaxTime(String time){
        if(time == null || "".equals(time.trim())){
            return time;
        }
        if(time.length() <= 10){
            time = time + " " + "23:59:59";
        }
        return time;
    }

    /**
     * 获取几月前的时间 on 2020/2/28 11:21
     *
     * @param date date
     * @param month month
     * @return Date
     */
    public static Date getMonthBefore(Date date,int month){
        Calendar now =Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.MONTH,now.get(Calendar.MONTH) - month);
        return now.getTime();
    }

    /**
     * 获取几月后的时间 on 2020/2/28 11:21
     *
     * @param date date
     * @param month month
     * @return Date
     */
    public static Date getMonthAfter(Date date,int month){
        Calendar now =Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.MONTH,now.get(Calendar.MONTH) + month);
        return now.getTime();
    }

    /**
     * 获取几年前的时间 on 2020/2/28 11:21
     *
     * @param date date
     * @param month month
     * @return Date
     */
    public static Date getYearBefore(Date date,int month){
        Calendar now =Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.YEAR,now.get(Calendar.YEAR) - month);
        return now.getTime();
    }

    /**
     * 获取几年后的时间 on 2020/2/28 11:21
     *
     * @param date date
     * @param month month
     * @return Date
     */
    public static Date getYearAfter(Date date,int month){
        Calendar now =Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.YEAR,now.get(Calendar.YEAR) + month);
        return now.getTime();
    }

    /**
     * 获取当前时间到月底的天数 on 2020/2/28 11:35
     *
     * @param date date
     * @return int
     */
    public static int leaveEndMonthDay(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int d = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        int now = c.get(Calendar.DAY_OF_MONTH);
        return d - now;
    }

    /**
     * 取得季度月 on 2020/2/28 12:08
     *
     * @param date date
     * @param num 该参数为正数可以获取之后季度,为负数获取之前季度
     * @return Date[]
     */
    public static Date[] getSeasonDate(Date date,Integer num) {
        Date[] season = new Date[3];
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int nSeason = getSeason(date);
        if(num != null){
            nSeason += num;
        }
        if (nSeason == 1) {
            // 第一季度
            c.set(Calendar.MONTH, Calendar.JANUARY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.FEBRUARY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MARCH);
            season[2] = c.getTime();
        } else if (nSeason == 2) {
            // 第二季度
            c.set(Calendar.MONTH, Calendar.APRIL);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MAY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.JUNE);
            season[2] = c.getTime();
        } else if (nSeason == 3) {
            // 第三季度
            c.set(Calendar.MONTH, Calendar.JULY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.AUGUST);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.SEPTEMBER);
            season[2] = c.getTime();
        } else if (nSeason == 4) {
            // 第四季度
            c.set(Calendar.MONTH, Calendar.OCTOBER);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.NOVEMBER);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.DECEMBER);
            season[2] = c.getTime();
        }
        return season;
    }

    /**
     * 获取本月是第几季度:1 第一季度 2 第二季度 3 第三季度 4 第四季度 on 2020/2/28 12:07
     *
     * @param date date
     * @return int
     */
    public static int getSeason(Date date) {
        int season = 0;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        switch (month) {
            case Calendar.JANUARY:
            case Calendar.FEBRUARY:
            case Calendar.MARCH:
                season = 1;
                break;
            case Calendar.APRIL:
            case Calendar.MAY:
            case Calendar.JUNE:
                season = 2;
                break;
            case Calendar.JULY:
            case Calendar.AUGUST:
            case Calendar.SEPTEMBER:
                season = 3;
                break;
            case Calendar.OCTOBER:
            case Calendar.NOVEMBER:
            case Calendar.DECEMBER:
                season = 4;
                break;
            default:
                break;
        }
        return season;
    }
}